
#include <iostream>
#include <thread>
#include <future>
#include <chrono>

int Add(int num1, int num2)
{
    std::cout << std::this_thread::get_id() << std::endl;
    std::cout << "加法11111111" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(10));
    std::cout << "加法22222222" << std::endl;
    return num1 + num2;
}




int main()
{
    std::cout << std::this_thread::get_id() << std::endl;
    //deferred -- 在执行get获取异步<<结果>>的时候，才会执行异步任务 (不创建新线程)
    //async    -- 内部会创建工作线程，异步的去完成任务 (会创建新线程)
    std::cout << "-------------------1------------------" << std::endl;
    //std::future<int> result = std::async(std::launch::deferred, Add, 1, 2);
    std::future<int> result = std::async(std::launch::async, Add, 1, 2);
    std::this_thread::sleep_for(std::chrono::seconds(20));
    std::cout << "-------------------2------------------" << std::endl;
    int sum = result.get(); //自带同步机制
    std::cout << "-------------------3------------------" << std::endl;
    std::cout << sum << std::endl;
    return 0;
}



// void Add(int num1, int num2, std::promise<int>& prom)
// {

// }


// int main()
// {

//     std::promise<int> prom;
//     std::future<int> fu = prom.get_future();

//     int sum;

//     return 0;
// }